package com.cosc716.restaurant.util;

import java.util.Scanner;

import com.cosc716.restaurant.domain.MenuItem;

public class UserInput {
	
	public static final Scanner SYSTEM_IN_SCANNER = new Scanner(System.in);
	
	public static final String YES = "y";
	public static final String NO = "n";
	
	public static MenuItem getMenuItem(Scanner scanner) {
		String name = getString("Enter name: ", scanner);
		double price = getDouble("Enter price: ", scanner);
		String ingredient = getString("Enter main ingredient: ", scanner);
		double calories = getDouble("Enter calories: ", scanner);
		double rating = getDoubleInRange("Enter rating(1-5): ", scanner, 1.0, 5.0);
		boolean isHeartHealthy = 
				(equalsYes(getYesNo("Is item heart healthy(y/n)?", scanner)))?
						(true):
						(false);
		
		return
			new MenuItem(name,price,ingredient,calories,rating,isHeartHealthy);
	}

	public static String getString(String prompt, Scanner scanner) {
		System.out.println(prompt);
		String returnValue = scanner.nextLine();
		
		return returnValue.trim();
	}
	
	public static int getInt(Scanner scanner) {
		while(true) {
			//System.out.println(prompt);
			String input = scanner.nextLine();
			try {
				return Integer.parseInt(input);
			} catch (NumberFormatException nfe) {
				System.out.println("Invalid input: Must enter a number.");
			}
		}
	}
	
	public static double getDouble(String prompt, Scanner scanner) {
		while(true) {
			System.out.println(prompt);
			String input = scanner.nextLine();
			try {
				return Double.parseDouble(input);
			} catch (NumberFormatException nfe) {
				System.out.println("Invalid input: Must enter a number.");
			}
		}
	}
	
	public static double getDoubleInRange(String prompt, Scanner scanner,
								double lowerBoundary, double upperBoundary) {
		while(true) {
			System.out.println(prompt);
			String input = scanner.nextLine();
			try {
				double inputDouble = Double.parseDouble(input);
				
				if( (inputDouble >= lowerBoundary) && (inputDouble <= upperBoundary) ) {
					return inputDouble;
				}
				else {
					System.out.println(
						String.format("Invalid input: Value must be between %d & %d(inclusive).",
										lowerBoundary, upperBoundary));
				}
			} catch (NumberFormatException nfe) {
				System.out.println("Invalid input: Must enter a number.");
			}
		}
	}
	
	public static String getSingleCharacter(String prompt, Scanner scanner) {
		String input = getString(prompt, scanner);
		input = input.substring(0, 1);
		
		return input;
	}
	
	public static String getYesNo(String prompt, Scanner scanner) {
		String input = getSingleCharacter(prompt, scanner);
		
		while(true) {
			if( input.equalsIgnoreCase("y") || input.equalsIgnoreCase("n") ) {
				return input;
			}
			else {
				System.out.println("Invalid input: Must provide yes/no answer.");
			}
		}
	}
	
	public static boolean equalsYes(String value) {
		value = value.trim().substring(0,1).toLowerCase();
		
		return value.equalsIgnoreCase(YES);
	}
	
	public static boolean equalsNo(String value) {
		value = value.trim().substring(0,1).toLowerCase();
		
		return value.equalsIgnoreCase(NO);
	}
}
